package me.chenzhijun.chapter2.service;

import me.chenzhijun.chapter2.db.DbUtils;
import me.chenzhijun.chapter2.entity.Customer;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

/**
 * @author chen
 * @version V1.0
 * @date 2017/9/27
 */
public class CustomerService {
    public List<Customer> getCustomerList() {
        return null;
    }

    public Customer addCustomer(Customer customer) throws SQLException {
        Connection connection = DbUtils.getConnection();
        String sql = "insert into customer(id,name,address) VALUES (?,?,?);";
        try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
            preparedStatement.setLong(1, customer.getId());
            preparedStatement.setString(2, customer.getName());
            preparedStatement.setString(3, customer.getAddress());

            int i = preparedStatement.executeUpdate();
            if (i > 0) {
                return customer;
            }
        }finally {
            DbUtils.closeConnection(connection);
        }
        return null;
    }


}
